#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//****************************************************************************************************//

struct hashdata
{	
	int ascii;			//to match the ascii code
	char username[25];		//to match the username
	char password[25];		//to match the password
}hashobject[100];


//****************************************************************************************************//

int linearprobing(int key,char username_l[25],char password_l[25],int m,int i)   //key is ascii value calculated by read username
{

int k,hashvalue;

hashvalue = (key + i)%m;
	if (hashobject[hashvalue].ascii==0)
		{	
			hashobject[hashvalue].ascii=key;
			strcpy(hashobject[hashvalue].username,username_l);			
			strcpy(hashobject[hashvalue].password,password_l);
		}
		
	else
		{
			i=i+1;
			linearprobing(key,username_l,password_l,100,i);
		}

}

//****************************************************************************************************//

int betterlinearprobing(int key,char username_l[25],char password_l[25],int m,int i)   //key is ascii value calculated by read username
{

int k,hashvalue,len;

len=strlen(username_l);
hashvalue = ((key%m)*len + i)%m;
	if (hashobject[hashvalue].ascii==0)
		{	
			hashobject[hashvalue].ascii=key;
			strcpy(hashobject[hashvalue].username,username_l);			
			strcpy(hashobject[hashvalue].password,password_l);
		}
		
	else
		{
			i=i+1;
			betterlinearprobing(key,username_l,password_l,100,i);
		}

}


//****************************************************************************************************//

int usermenulinear()

{
char username[25],password[25],c;
int i=0,j=0,k=0,ascii=-10,hashvalue,flag=1,flag2=1,m=100;

printf("Welcome to DSA hashing assignment !! :) \n");


printf("Please enter your USERNAME \n");

fgets(username,25,stdin);

while(username[j]!='\0')
	{
		c=username[j];
		ascii=ascii + c;
		j++;
	}
printf("The ascii value is : %d \n",ascii);
	
while(k<3)
{
printf("Please enter your PASSWORD \n");

fgets(password,25,stdin);
hashvalue = (ascii + i)%m;
	if (hashobject[hashvalue].ascii == ascii + i)
		{
			flag=strcmp(hashobject[hashvalue].username,username);

			//puts(hashobject[hashvalue].username);
				if(flag==0)

						flag2=strcmp(hashobject[hashvalue].password,password);
							if(flag2==0)
								{
								printf("Password correct \n");
								exit(0);	
								}
							else
								{
								printf("Password incorrect,Enter again! \n");
								continue;
								}			
		
					}

		}
	else
		{
			i=i+1;
		}
		

k++;
}
return 0;
}


//****************************************************************************************************//

int usermenubetter()

{
char username[25],password[25],c;
int i=0,j=0,k=0,ascii=0,hashvalue,len,flag=1,flag2=1,m=100;

printf("Welcome to DSA hashing assignment !! :) \n");


printf("Please enter your USERNAME \n");

fgets(username,25,stdin);
len=strlen(username);

while(username[j]!='\0')
	{
		c=username[j];
		ascii=ascii + c;
		j++;
	}
	
while(k<3)
{
printf("Please enter your PASSWORD \n");

fgets(password,25,stdin);
hashvalue =((ascii%m)*len + i)%m;
	if (hashobject[hashvalue].ascii == ascii + i)
		{	
			flag=strcmp(hashobject[hashvalue].username,username);
				if(flag=0)
					{
						flag2=strcmp(hashobject[hashvalue].password,password);
							if(flag2=0)
								{
								printf("Password correct \n");
								exit(0);	
								}
							else
								{
								printf("Password incorrect \n");
								continue;
								}			
		
					}
		}
	else
		{
			i=i+1;
		}
		

k++;
}
return 0;
}

//****************************************************************************************************//
//****************************************************************************************************//
int main()
{
	FILE *file;			//file handling implementation
	char c,username_t[25],password_t[25];   //Temporary username and password holders..
	int i=0,l,k,y,j=0,ascii_t=0;
	int new_line=0,space=0;
	//considering the hashtable's size,m=100
	
	for(l=0;l<100;l++)                     //initialising the ascii values of all structure objects as 0
	{
	hashobject[l].ascii=0;
	}
	
	file = fopen("usernames.txt","r");         //assuming that the name of the name of the file as usernames.txt

	
		if (file==NULL)                                   //checking invalid file
			{
			printf("File does not exist ! \n");
			}
	c = getc(file);
	while(c!=EOF)		
			{
				
				if(new_line==0)
						{
						if(space==0)
						{
							if(c!=' ')
							{
								username_t[i]=c;
								ascii_t = ascii_t + c;
							
								i=i+1;
							}
							else
							{	space=1;
								username_t[i]='\0';
								i=0;	
								
							}
						}
						
						
						else
						{
							if(c!='\n')
							{
								password_t[j]=c;
								j=j+1;
							}
							else
							{	new_line=1;
								password_t[j]='\0';
								j=0;
								space=0;
							}
						}
						}

					
					if(new_line==1)
						{	new_line=0;
							

							linearprobing(ascii_t,username_t,password_t,100,0);
							
							ascii_t=0;

						}
					c = getc(file);
					}
					
for(y=0;y<100;y++)
{
printf("%d %d\n",y,hashobject[y].ascii);
puts(hashobject[y].username);
puts(hashobject[y].password);
}						

usermenulinear();
					
return 0;
}	


